import { useState, useEffect } from 'react';
import { getmyprocessjsonbydeploymentid } from '@/services/flowMine/apply';
import FORM_TYPE_MAP from '../consts';

const gradeTypes = [
    FORM_TYPE_MAP.ADD_GRADE,
    FORM_TYPE_MAP.ADD_GRADE_TYPE,
    FORM_TYPE_MAP.EDIT_GRADE,
    FORM_TYPE_MAP.EDIT_GRADE_TYPE,
    FORM_TYPE_MAP.REMOVE_GRADE,
    FORM_TYPE_MAP.REMOVE_GRADE_TYPE
];

/**
 * 获取审批进度
 */
const useProcess = (formValueJson, processDeploymentId, applicationNo, formType) => {
    const [process, setProcess] = useState([]);
    const formValue = JSON.parse(formValueJson);

    const fetchProcess = async () => {
        // 注意：职级中，需要制度类型用于请求
        if (!formValue.institutionType && gradeTypes.includes(formType)) {
            return;
        }
        if (processDeploymentId) {
            const extra = formValue.recruitPost ? { recruitPost: String(formValue.recruitPost) } : {};
            const formValues = { ...formValue, ...extra };
            const result = await getmyprocessjsonbydeploymentid(JSON.stringify(formValues), processDeploymentId);
            if (result.code === 10000) {
                console.log('===============================================>调试 审批进度', result.data);
                setProcess(result.data);
            }
        }
    };

    useEffect(() => {
        if (formValueJson !== '{}') {
            fetchProcess();
        }
    }, [formValueJson, processDeploymentId, applicationNo]);

    return [process];
};

export default useProcess;
